package com.kaiwukj.android.ufamily.mvp.ui.page.service.community;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.ui.widget.home.ShopWebView;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.CommunityServiceWeb)
public class CommunityServiceWebActivity extends BaseActivity {

    public static final int NEWS = 1;
    public static final int ZYZM = 2;
    public static final int GUIDE = 3;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.web)
    ShopWebView mWebView;


    @Autowired
    String url;
    @Autowired
    int type;
    @Autowired
    int articleId;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }


    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_service_web;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.GONE);
        // mWebView.loadUrl("http://192.168.188.82:6677/InformationInfo.html");
        // mWebView.loadUrl("http://committeeh5test.jiayuanli.net/InformationInfo.html");
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mWebView != null) {
                    if (type == NEWS) {
                        mWebView.initCommunityInfo(articleId, getApp().getUserInfo().getLastCommunityId());
                    } else if (type == ZYZM) {
                        mWebView.setToken(getApp().getUserInfo().getLastCommunityId());

                    } else {
                        mWebView.initCommunityGuide(getApp().getUserInfo().getLastCommunityId());
                    }
                }
            }
        });
        mWebView.setOnServiceLiner(new ShopWebView.OnServiceListener() {
            @Override
            public void onTokenInvalid() {

            }

            @Override
            public void onGoHome() {
                finish();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onBackPressedSupport() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressedSupport();
        }
    }
}
